<template>
  <div class="app-container">
    <el-row :gutter="20">
      <splitpanes :horizontal="appStore.device === 'mobile'" class="default-theme">
        <!--用户数据-->
        <pane size="84">
          <el-col>
            <el-form :model="queryParams" ref="queryRef" :inline="true" label-width="68px">
              <el-form-item label="档口" prop="dangKou" style="width: 240px">
                <el-select v-model="queryParams.dangKouId" placeholder="请选择">
                  <el-option v-for="item in dangKouList" :key="item.id" :label="item.name" :value="item.id" ></el-option>
                </el-select>
              </el-form-item>
              <el-form-item label="仓库" prop="cangKu" style="width: 240px">
                <el-select v-model="queryParams.cangKuId" placeholder="请选择">
                  <el-option v-for="item in cangKuList" :key="item.id" :label="item.name" :value="item.id" ></el-option>
                </el-select>
              </el-form-item>
              <el-form-item label="创建时间" style="width: 308px">
                <el-date-picker v-model="dateRange" value-format="YYYY-MM-DD" type="daterange" range-separator="-" start-placeholder="开始日期" end-placeholder="结束日期"></el-date-picker>
              </el-form-item>
              <el-form-item>
                <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
                <el-button icon="Refresh" @click="resetQuery">重置</el-button>
              </el-form-item>
            </el-form>

            <el-row :gutter="10" class="mb8">
            </el-row>

            <el-table v-loading="loading" :data="dingDanList" @selection-change="handleSelectionChange">
              <el-table-column type="selection" width="50" align="center" />
              <el-table-column label="订单编号" align="center" key="id" prop="id"/>
              <el-table-column label="档口" align="center" key="dangKouName" prop="dangKouName"/>
              <el-table-column label="仓库" align="center" key="cangKuName" prop="cangKuName" />
              <el-table-column label="日期" align="center" >
                <template #default="scope">
                  <span>{{ parseTime(scope.row.riQi, "{y}-{m}-{d}") }}</span>
                </template>
              </el-table-column>
              <el-table-column label="审核状态" align="center" >
                <template #default="scope">
                  <el-text v-if="scope.row.status === 0">审核中</el-text>
                  <el-text v-if="scope.row.status === 1">通过</el-text>
                  <el-text v-if="scope.row.status === 2">驳回</el-text>
                </template>
              </el-table-column>
              <el-table-column label="原因" align="center" key="statusReason" prop="statusReason" />
              <el-table-column label="创建时间" align="center" prop="createTime" width="160">
                <template #default="scope">
                  <span>{{ parseTime(scope.row.createTime) }}</span>
                </template>
              </el-table-column>
              <el-table-column label="操作" align="center" width="150" class-name="small-padding fixed-width">
                <template #default="scope">
                  <el-tooltip content="审核" placement="top" v-if="scope.row.status === 0">
                    <el-button link type="primary" icon="Edit" @click="showShenHe(scope.row)"></el-button>
                  </el-tooltip>
                  <el-tooltip content="撤销" placement="top" v-if="scope.row.status === 0">
                    <el-button link type="primary" icon="Edit" @click="chexiao(scope.row)"></el-button>
                  </el-tooltip>
                </template>
              </el-table-column>
            </el-table>
            <pagination v-show="total > 0" :total="total" v-model:page="queryParams.pageNum" v-model:limit="queryParams.pageSize" @pagination="getList" />
          </el-col>
        </pane>
      </splitpanes>
    </el-row>

    <!-- 添加或修改用户配置对话框 -->
    <el-dialog :title="title" v-model="open" width="600px" append-to-body>
      <el-form :model="form" :rules="rules" ref="userRef" label-width="80px">
        <el-row>
          <el-col :span="12">
            <el-form-item label="档口" prop="address">
              <el-text>{{form.dangKouName}}</el-text>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="仓库" prop="name">
              <el-text>{{form.cangKuName}}</el-text>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item label="货品列表" prop="name">
              <el-table :data="form.list">
                <el-table-column prop="id" label="编号"/>
                <el-table-column label="名称">
                  <template #default="scope">
                    <el-text >{{scope.row.huoPin.name}}</el-text>
                  </template>
                </el-table-column>
                <el-table-column prop="buyCount" label="购买数量"/>
                <el-table-column label="单价">
                  <template #default="scope">
                    <el-text >{{scope.row.huoPin.price}}</el-text>
                  </template>
                </el-table-column>
                <el-table-column label="总价">
                  <template #default="scope">
                    <el-text >{{scope.row.huoPin.price * scope.row.buyCount}}</el-text>
                  </template>
                </el-table-column>
              </el-table>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="审核意见">
              <el-input v-model="form.statusReason"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm(1)">通 过</el-button>
          <el-button type="warning" @click="submitForm(2)">驳 回</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="DingDan">
import useAppStore from '@/store/modules/app'
import {getDingDan, listDingDan, shenhe} from "@/api/system/dingdan";
import { Splitpanes, Pane } from "splitpanes"
import "splitpanes/dist/splitpanes.css"
import {listAll as loadAllDangKou} from "@/api/system/dangkou.js";
import {listUserAll as loadAllCangKu} from "@/api/system/cangku.js"

const router = useRouter();
const appStore = useAppStore()
const { proxy } = getCurrentInstance();
const { sys_normal_disable, sys_user_sex } = proxy.useDict("sys_normal_disable", "sys_user_sex");

const open = ref(false);
const loading = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");
const dateRange = ref([]);
const dingDanList = ref([]);
const dangKouList = ref([]);
const cangKuList = ref([]);

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    dangKouId: undefined,
    cangKuId: undefined,
  },
  rules: {
    address: [{ required: true, message: "用户名称不能为空", trigger: "blur" }],
    name: [{ required: true, message: "用户昵称不能为空", trigger: "blur" }],
    adminId: [{ required: true, message: "请指定档口管理员", trigger: ["blur", "change"] }]
  }
});

const { queryParams, form, rules } = toRefs(data);

/** 查询用户列表 */
function getList() {
  loading.value = true;
  listDingDan(proxy.addDateRange(queryParams.value, dateRange.value)).then(res => {
    loading.value = false;
    dingDanList.value = res.rows;
    total.value = res.total;
  });
}
getList();

function initDangKouList(){
  loadAllDangKou().then(res => {
    dangKouList.value = res.data;
  });
}

initDangKouList();

function initCangKuList(){
  loadAllCangKu().then(res=>{
    cangKuList.value = res.rows;
  })
}

initCangKuList();

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  dateRange.value = [];
  proxy.resetForm("queryRef");
  queryParams.value.deptId = undefined;
  handleQuery();
}


/** 选择条数  */
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.userId);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
};

/** 重置操作表单 */
function reset() {
  form.value = {
    id: undefined,
    name: undefined,
    address: undefined,
    adminId: undefined,
    remark: '',
  };
  proxy.resetForm("userRef");
};

/** 取消按钮 */
function cancel() {
  open.value = false;
  reset();
};

/** 修改按钮操作 */
function showShenHe(row) {
  getDingDan(row.id).then(response => {
    form.value = response.data;
    open.value = true;
    title.value = "审核";
  });
}

/** 提交按钮 */
function submitForm(status) {

  shenhe(form.value.id, status, form.value.statusReason ).then(response => {
    proxy.$modal.msgSuccess("操作成功");
    open.value = false;
    getList();
  });

}

/** 撤销 */
function chexiao(row) {

  shenhe(row.id,"3", "" ).then(response => {
    proxy.$modal.msgSuccess("操作成功");
    open.value = false;
    getList();
  });

}

</script>
